package cn.doit.scala.day02.map

/**
 * @DATE 2021/12/25/17:04
 * @Author MDK
 * @Version 2021.2.2
 * */
object C01_Map02 {
  def main(args: Array[String]): Unit = {
    //定义Map集合两种方式
    val mp1 = Map[String, Int](
      ("zss",23),
      ("lss",33),
      "wbb"->24,
      "zg"->28
    )

    //遍历map几种方式
    //方式一----------------------------------对偶元组
    for (elem <- mp1) {
      println(s"key是:${elem._1} value是:${elem._2}")
    }

    //方式二----------------------------------keys
    val keys: Iterable[String] = mp1.keys
    for (key <- keys) {
      val maybeInt: Option[Int] = mp1.get(key)
      if(maybeInt.isDefined){
        val value: Int = maybeInt.get
        println(s"2    key是:$key value是:$value")
      }
    }

    //方式三----------------------------------(k,v)
    for ((k,v) <- mp1) {
      println(s"K: $k  V:$v")
    }

    //只打印k
    for ((k,_) <- mp1) {
      println(s"K: $k  V:")
    }

    //只打印v
    for ((_,v) <- mp1) {
      println(s"K: , V: $v")
    }
  }
}
